<?php

namespace app\common\logic;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\model\CarModels;
use app\common\model\Service;
use app\common\model\ServiceImage;
use app\common\model\ServiceShop;
use app\common\model\Shop;
use app\common\model\UsedCar;
use think\Exception;

class ServiceLogic extends BaseLogic
{
    public $model;

    public function assignModel()
    {
        $this->model          = new ServiceShop();
    }

    /**
     * 车主端商机类别列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function client_service()
    {
        return Service::getService('id,name,logo,select_logo,client is_add,desc');
    }

    /**
     * 获取商家端的商机类别列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shop_service($type=0)
    {
        $service = Shop::where('id',MemberVo::instance()->getShopId())->value('service'); //获取店铺可发布的商机
        $service_arr = explode(',',$service);
        if ($type == 0){
            /*$list = ServiceShop::where(['is_del'=>0,'type'=>2,'shop_id'=>MemberVo::instance()->getShopId()])->whereNotIn('service_type',$service_arr)->group('service_type')->column('service_type');//获取历史发布的商机类别
            $all_service_arr = array_merge($service_arr,$list);
            if (!in_array(1,$all_service_arr)){ //查看二手车
                $res = UsedCar::where(['is_del'=>0,'type'=>2,'shop_id'=>MemberVo::instance()->getShopId()])->find();
                if ($res)
                    array_push($all_service_arr,1);
            }*/
//            return $all_service_arr;
            $result = Service::field('id,name,logo,select_logo,shop is_add,desc')->where(['status'=>1])->order('sort asc')->select();
            if (!empty($result)){
                foreach ($result as &$v){
                    if (!in_array($v['id'],$service_arr))
                        $v['is_add'] = 0;
                }
            }
        }else{
            $result = Service::field('id,name,logo,select_logo,shop is_add,desc')->where(['status'=>1])->whereIn('id',$service_arr)->order('sort asc')->select();
        }
        return $result;
    }

    /**
     * 列表
     * @param array $params
     */
    public function index($params)
    {
        if ($params['type'] == 1)
            throw new ApiException('接口调用有误',404);
        return ServiceShop::getList($params);
    }

    /**
     * 二手车列表
     * @param array $params
     */
    public function used_car($params)
    {

        return UsedCar::getList($params);
    }

    public function car_edit($params)
    {
        try{
            $params['member_id'] = MemberVo::instance()->getMemberId();
            $shop_id = MemberVo::instance()->getShopId();
            $fields = 'cover_img,shop_id,member_id,type,down_payment,transfer_num,insurance_expires,brand_id,series_id,models_id,car_type,level,car_age,mileage,discharge,gearbox,swept_volume,insurance_expires,transfer_num,down_payment,price,phone,province,city,districts,address,longitude,latitude,detail,licence_city';
            //获取车辆品牌 系列
            $carModelsInfo = CarModels::get($params['models_id']);
            $params['brand_id'] = $carModelsInfo['brand_id'];
            $params['series_id'] = $carModelsInfo['series_id'];
            //手机号格式处理
            if (isset($params['phone_arr'])){
                $params['phone'] =  implode(',',$params['phone_arr']); //多电话格式
            }
            if (isset($params['id']) && !empty($params['id'])){ //更新
                UsedCar::update($params,['id'=>$params['id']],$fields);
                ServiceImage::where(['main_id'=>$params['id'],'service_type'=>1])->delete();
                if (!empty($params['images'])){
                    $arr = [];
                    foreach ($params['images'] as $v){
                        $arr[] = ['main_id'=>$params['id'],'image'=>$v,'service_type'=>1];
                    }
                    ServiceImage::insertAll($arr);
                }
            }else{ //添加
                if ($shop_id > 0){
                    $params['shop_id'] = $shop_id;
                    $params['type'] = 1;
                }
                $res = UsedCar::create($params,$fields);
                if (!empty($params['images'])){
                    $arr = [];
                    foreach ($params['images'] as $v){
                        $arr[] = ['main_id'=>$res->id,'image'=>$v,'service_type'=>1];
                    }
                    ServiceImage::insertAll($arr);
                }
            }
            return true;
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }














//        unset($params['ver']);
//
//        if (isset($params['id']) && !empty($params['id'])){
//            $info = UsedCar::get(['id'=>$params['id'],'member_id'=>MemberVo::instance()->getMemberId()]);
//            if (!$info)
//                throw new ApiException('查无该二手车记录');
//            UsedCar::update($params,['id'=>$params['id']]);
//            return true;
//        }
//        UsedCar::create($params);
//        return true;

    }

    public function edit($params)
    {
        unset($params['ver']);
        $params['member_id'] = MemberVo::instance()->getMemberId();
        if (isset($params['id']) && !empty($params['id'])){
            $info = ServiceShop::get(['id'=>$params['id'],'member_id'=>MemberVo::instance()->getMemberId()]);
            if (!$info)
                throw new ApiException('查无该条记录',404);
            ServiceShop::update($params,['id'=>$params['id']]);
            return true;
        }
        ServiceShop::create($params);
        return true;
    }

    /**
     * 获取车辆详情
     * @return ServiceShop|null
     * @throws ApiException
     * @throws \think\exception\DbException
     */
    public function car_detail($params)
    {
        $params['member_id'] = MemberVo::instance()->getMemberId();
        $info = UsedCar::get($params['id']);
        if (!$info)
            throw new ApiException('查无该车辆信息',404);
        $info['edit'] = 0;
        if ($info['member_id'] == MemberVo::instance()->getMemberId()){
            $info['edit'] = 1;
        }
        return $info;
    }

    /**
     * 商机详情
     * @return ServiceShop|null
     * @throws ApiException
     * @throws \think\exception\DbException
     */
    public function detail($params)
    {
        $params['member_id'] = MemberVo::instance()->getMemberId();
        $info = ServiceShop::get($params['id']);
        if (!$info)
            throw new ApiException('查无该商机信息',404);
        $info['edit'] = 0;
        if ($info['member_id'] == MemberVo::instance()->getMemberId()){
            $info['edit'] = 1;
        }
        return $info;
    }


}